package adminService

import (
	"gin/chibuda/app/utils"
	"strings"
)

func ArticleList(data map[string]string) utils.Service{
	if data["page"] == "" {
		data["page"] = "1"
	}
	if data["pageCount"] == "" {
		data["pageCount"] = "10"
	}
	query := Db().Table("article")
	if data["keyWord"] != ""{
		query.Where("title","like","%"+data["keyWord"]+"%").OrWhere("subtitle","like","%"+data["keyWord"]+"%")
	}
	list , _ :=query.
		Limit(utils.Int(data["pageCount"])).
		Paginate(utils.Int(data["page"]))
	return utils.Success(list)
}

func ArticleEdit(data map[string]string) utils.Service{
	if data["id"] != "" {
		_,err := Db().Table("article").
			Where("id",data["id"]).
			Data(data).
			Update()
		if err!=nil {
			return utils.Error(err)
		}
	}else{
		delete(data,"id")
		data["create_time"] = utils.String64(utils.Time())
		res,err := Db().Table("article").
			Data(data).
			Insert()
		if err!=nil || res == 0 {
			return utils.Error("添加失败")
		}
	}
	return utils.Success()
}

func ArticleDel (data map[string]string) utils.Service{
	idArr := strings.Split(data["id"],",")
	var idArrIntFace []interface{}
	for _,v:=range idArr {
		idArrIntFace=append(idArrIntFace,v)
	}
	res,err:=Db().Table("article").
		WhereIn("id", idArrIntFace).
		Delete()
	if err!=nil || res == 0 {
		return utils.Error("删除失败")
	}
	return utils.Success()
}